package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.DateFormatUtil;
import com.ccp.dev.core.util.StringPool;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdFailureAnalysisInfoDao;
import com.ccp.dev.qbdms.model.WQbdFailureAnalysisInfo;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 失效分析 信息操作服务类
 *
 * @author zzy
 * @date 2020-07-07 10:43:27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdFailureAnalysisInfoService extends BaseService<WQbdFailureAnalysisInfo> {

    @Resource
    private WQbdFailureAnalysisInfoDao dao;
    @Resource
    private ScriptImpl script;


    /**
     * 获取上报信息
     * @param id
     * @param table
     * @return
     */
    public List<WQbdFailureAnalysisInfo> reportList(String id,String tablename){
        return dao.reportList(id, tablename);
    }

    /**
     * 查询全部,导出用
     * @param queryFilter
     * @return
     */
    public List<WQbdFailureAnalysisInfo> queryAll(QueryFilter queryFilter){
        return dao.queryAll(queryFilter);
    }

    /**
     * 导入新增
     * @param list  数据集
     * @throws Exception 异常
     */
    public void addImportData(List<WQbdFailureAnalysisInfo> list) throws Exception{
        String path = BaseUtil.getCurrentTenOrgCodePath();
        Date date = new Date();
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
        String userId = sysUser.getUserId();
        String tenantId = ContextUtil.getCurrentUserTenantId();
        SysOrg sysOrg = (SysOrg) ContextUtil.getCurrentOrg();
        String code = sysOrg.getCode();
        String orgName = sysOrg.getOrgName();
        String fillMan = sysUser.getFullName();
        String account = sysUser.getAccount();

        list.forEach(info->{
            String mainId = ProjectUUIDUtils.getUUIDFor32();
            info.setId(mainId);
            info.setTenantId(tenantId);
            info.setCreateBy(userId);
            info.setCreateTime(date);
            info.setFilldept(orgName);
            info.setFilldeptid(code);
            info.setFillman(fillMan);
            info.setFillmanaccount(account);
            info.setFilldate(DateFormatUtil.format(date, StringPool.DATE_FORMAT_DATE));
            info.setReportStatus(ZhiLiangConst.REPORT_STATUS_GROUPTOBEREPORTED);
            if(StringUtil.isEmpty(info.getBelongdeptname())){
                info.setBelongdeptname(path);
            }
        });
        if(!list.isEmpty()){
            dao.addList(list);
        }
    }

    /**
     * 用于上报获取数据
     * @param dataId
     * @return
     */
    public WQbdFailureAnalysisInfo getParentAndChilden(String dataId) {
        return dao.getById(dataId);
    }

    /**
     * 接收上报来的数据
     * @param data
     */
    public void saveAll(List<WQbdFailureAnalysisInfo> data,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(data,extendSysFileList);
        for (WQbdFailureAnalysisInfo model : data) {
            // 如果上报数据中hospital为空,认为是三级上报来的,如果不为空,则认为是二级上报来的
            if (StringUtil.isEmpty(model.getFilldeptHospitalId())) {
                model.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(model.getFilldeptid()));
            }
            else{
                model.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(model.getFilldeptHospitalId()));
            }
            WQbdFailureAnalysisInfo temp = dao.getById(model.getId());
            if (BeanUtils.isNotEmpty(temp)) {
                update(model);
            }
            else{
                add(model);
            }
        }
    }


}
